// pages/user/user.ts
interface UserInfo {
  type?: number,
  name?: string,
  avatarUrl?: string,
  sex?: number,
  grade?: string,
  studentId?: string,
  subject?: string,
  publish?: number,
  liked?: number,
  subscribe?: number,
  collect?: number,
}
enum UserType {
  STUDENT = 0,
  PARENTS,
  TEACHER,
  CUSTOMER,
}

enum SexType {
  MALE = 0,
  FEMALE,
}

const userType = {
  student: UserType.STUDENT,
  parents: UserType.PARENTS,
  teacher: UserType.TEACHER,
  customer: UserType.CUSTOMER,
}

const sexType = {
  male: SexType.MALE,
  female: SexType.FEMALE,
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userType: userType,
    sexType: sexType,
    userInfo: {
      userType: 0,
      name: "",
      avatarUrl: "/images/avatar-default-icon.png",
      sex: 0,
      grade: "",
      studentId: "",
      subject: "",
      publish: 0,
      liked: 0,
      subscribe: 0,
      collect: 0,
    } as UserInfo
  },
  handleOperate(e: any) {
    const { type } = e.currentTarget.dataset;
    if (type === 'work') {
      wx.navigateTo({
        url: '/package-user/pages/favorite-work/favorite-work'
      })
    }
    else if (type === 'message') {
      wx.navigateTo({
        url: '/package-homework/pages/home-school-chat/home-school-chat'
      })
    }
    else if (type === 'document') {
      wx.navigateTo({
        url: '/package-group/pages/home-school-export/home-school-export'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.request({
      url: "https://mock.presstime.cn/mock/656dbe322fef936d34630d40/yi-xiao-ping/user-info",
      success: (res: {
        data: {
          code: number,
          data: UserInfo
        }
      }) => {
        const { data } = res
        const { code, data: userData } = data
        if (code === 200) {
          this.setData({
            userInfo: {
              ...userData,
            }
          })
        }
      }
    })
  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
  },
  handleInformation() {
    wx.navigateTo({
      url: '/package-user/pages/user-information/user-information'
    })
  },
  handleAvatar(e: any) {
    const { avatarUrl } = e.detail;
    this.setData({
      userInfo: {
        ...this.data.userInfo,
        avatarUrl: avatarUrl,
      }
    })
    // 处理选择头像的逻辑
  },
})